package com.hjqjl.whdemo.service;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;

import androidx.core.app.NotificationCompat;

import com.hjqjl.whdemo.R;

import java.io.IOException;

public class MusicService extends Service {
    //    private String path = "mnt/sdcard/water.mp3";
//    private String path = Environment.getExternalStorageDirectory() + "/water.mp3";
    private MediaPlayer player;
    private MediaPlayer playerNetWork;

    @Override
    public IBinder onBind(Intent intent) {
        //当执行完了onCreate后，就会执行onBind把操作歌曲的方法返回
        return new MyBinder();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        //这里只执行一次，用于准备播放器
//        player = new MediaPlayer();
        player = MediaPlayer.create(this, R.raw.water);
//        try {
//            player.setDataSource(path);
//            //准备资源
//            player.prepare();
//            Log.e("服务", "准备播放音乐加载地址正确");
//        } catch (IOException e) {
//            Log.e("服务", "准备播放音乐加载地址错误");
//            e.printStackTrace();
//        }
//        Log.e("服务", "准备播放音乐"+path);


        /**
         * Oreo不用Priority了，用importance
         * IMPORTANCE_NONE 关闭通知
         * IMPORTANCE_MIN 开启通知，不会弹出，但没有提示音，状态栏中无显示
         * IMPORTANCE_LOW 开启通知，不会弹出，不发出提示音，状态栏中显示
         * IMPORTANCE_DEFAULT 开启通知，不会弹出，发出提示音，状态栏中显示
         * IMPORTANCE_HIGH 开启通知，会弹出，发出提示音，状态栏中显示
         */
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder ;
        String CHANNEL_ID = "my_channel_01";
        if (Build.VERSION.SDK_INT >= 26){        //Android 8.0适配
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
                    "Channel human readable title",
                    NotificationManager.IMPORTANCE_DEFAULT);//如果这里用IMPORTANCE_NOENE就需要在系统的设置里面开启渠道， //通知才能正常弹出
            manager.createNotificationChannel(channel);
            builder = new NotificationCompat.Builder(this,String.valueOf(CHANNEL_ID));
        }else{
            builder = new NotificationCompat.Builder(this);
        }
        builder.setContentTitle("this is content title")            //指定通知栏的标题内容
                .setContentText("this is content text")             //通知的正文内容
                .setWhen(System.currentTimeMillis())                //通知创建的时间
                .setSmallIcon(R.drawable.ic_launcher_background)    //通知显示的小图标，只能用alpha图层的图片进行设置
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher_background));

        Notification notification = builder.build() ;
        startForeground(1, notification);
    }

    @Override
    public void onDestroy() {
        stopForeground(true);// 停止前台服务
        super.onDestroy();
    }

    //该方法包含关于歌曲的操作
    public class MyBinder extends Binder {

        //判断是否处于播放状态
        public boolean isPlaying() {
            return player.isPlaying();
        }

        //播放或暂停歌曲
        public void play() {
            if (player.isPlaying()) {
                player.pause();
            } else {
                player.start();
            }
            Log.e("服务", "播放音乐");
        }

        //返回歌曲的长度，单位为毫秒
        public int getDuration() {
            return player.getDuration();
        }

        //返回歌曲目前的进度，单位为毫秒
        public int getCurrenPostion() {
            return player.getCurrentPosition();
        }

        //设置歌曲播放的进度，单位为毫秒
        public void seekTo(int mesc) {
            player.seekTo(mesc);
        }

        //播放网络歌曲
        public void playNetWorkMusic() {
            if (playerNetWork == null) {
                //如果为空就new我一个
                playerNetWork = new MediaPlayer();
                try {
                    playerNetWork.setDataSource("https://www.w3school.com.cn/i/horse.mp3");//https，一定要加s，不然闪退
                    //异步准备
                    playerNetWork.prepareAsync();
                    //添加准备好的监听
                    playerNetWork.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mp) {
                            //如果准备好了，就会进行这个方法
                            mp.start();
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                //判断是否处于播放状态
                if (playerNetWork.isPlaying()) {
                    playerNetWork.pause();
                } else {
                    playerNetWork.start();
                }
            }
        }
    }
}
